<template>
  <div class="container">
    <div class="container-title">
      <p>项目授权</p>
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item>招商管理</el-breadcrumb-item>
        <el-breadcrumb-item>招商配置</el-breadcrumb-item>
        <el-breadcrumb-item>项目授权</el-breadcrumb-item>
      </el-breadcrumb>
    </div>

    <div class="search-box">
      <el-form ref="form" :model="searchData">
        <el-form-item
          ><el-input
            v-model="searchData.authorizationPut"
            placeholder="权限授出部门"
          ></el-input>
        </el-form-item>

        <el-form-item>
          <el-button
            type="success"
            @click="onSearch"
            icon="el-icon-search"
            style="background-color: #0bb197"
            >查询</el-button
          ><el-button @click="onReset" icon="el-icon-refresh" class="reset"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
    </div>

    <!-- 表格 -->
    <div class="data-list">
      <div class="data-list-title">
        <span>查询结果列表</span>
        <el-button
          type="success"
          style="background-color: #0bb197"
          @click="addOpen"
          >新增</el-button
        >
      </div>
      <el-table :data="tableData" stripe style="width: 100%">
        <el-table-column prop="id" label="序号" align="center">
        </el-table-column>
        <el-table-column
          prop="authorizationPut"
          label="权限授出部门"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="authorizationGet"
          label="被授权部门"
          align="center"
        >
        </el-table-column>
        <el-table-column prop="empName" label="被授权人员" align="center">
        </el-table-column>
        <el-table-column label="操作" align="center" width="200">
          <template slot-scope="scope">
            <el-button @click="editOpen(scope.row)" type="text" size="small"
              >编辑</el-button
            >
            <el-button @click="delOpen(scope.row)" type="text" size="small"
              >删除</el-button
            >
          </template></el-table-column
        >
      </el-table>
      <!-- 页码 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="searchData.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        :background="true"
      >
      </el-pagination>
    </div>

    <!-- 新增弹窗 -->
    <el-dialog
      width="43%"
      title="新增授权"
      :visible.sync="addDialog"
      append-to-body
      class="add"
    >
      <el-form :model="addData">
        <div class="authorized-department">
          <el-form-item label="权限授出部门">
            <el-select v-model="addData.authorizationPut" placeholder="请选择">
              <el-option
                v-for="item in authorizationPutOptions"
                :key="item.value"
                :label="item.label"
                :value="item.label"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="被授权部门">
            <el-select v-model="addData.authorizationGet" placeholder="请选择">
              <el-option
                v-for="item in authorizationGetOptions"
                :key="item.value"
                :label="item.label"
                :value="item.label"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </div>
        <div class="authorized-personnel">
          <el-form-item label="被授权人员">
            <el-select v-model="addData.empName" placeholder="请选择">
              <el-option
                v-for="item in empNameOptions"
                :key="item.value"
                :label="item.label"
                :value="item.label"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addDialog = false" class="cancel">取 消</el-button>
        <el-button type="primary" @click="addSave" class="save"
          >保 存</el-button
        >
      </div>
    </el-dialog>

    <!-- 编辑弹窗 -->
    <el-dialog
      width="43%"
      title="编辑授权"
      :visible.sync="editDialog"
      append-to-body
      class="edit"
    >
      <el-form :model="editData">
        <div class="authorized-department">
          <el-form-item label="权限授出部门">
            <el-select v-model="editData.authorizationPut" placeholder="请选择">
              <el-option
                v-for="item in authorizationPutOptions"
                :key="item.value"
                :label="item.label"
                :value="item.label"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="被授权部门">
            <el-select v-model="editData.authorizationGet" placeholder="请选择">
              <el-option
                v-for="item in authorizationGetOptions"
                :key="item.value"
                :label="item.label"
                :value="item.label"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </div>
        <div class="authorized-personnel">
          <el-form-item label="被授权人员">
            <el-select v-model="editData.empName" placeholder="请选择">
              <el-option
                v-for="item in empNameOptions"
                :key="item.value"
                :label="item.label"
                :value="item.label"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editPlanDialog = false" class="cancel"
          >取 消</el-button
        >
        <el-button type="primary" @click="editSave" class="save"
          >保 存</el-button
        >
      </div>
    </el-dialog>

    <!-- 删除弹窗 -->
    <el-dialog :visible.sync="delDialog" width="21%" center class="del">
      <span>数据删除将无法恢复，请问确认删除吗？</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="delDialog = false" class="cancel">取 消</el-button>
        <el-button type="primary" @click="delSave" class="save"
          >保 存</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data: function () {
    return {
      total: 0,
      currentPage: 1,
      authorizationPutOptions: [
        {
          value: '0',
          label: '广州分公司',
        },
        {
          value: '1',
          label: '成都分公司',
        },
        {
          value: '2',
          label: '北京分公司',
        },
      ],
      authorizationGetOptions: [
        {
          value: '0',
          label: '广州分公司招商部',
        },
        {
          value: '1',
          label: '成都分公司招商部',
        },
        {
          value: '2',
          label: '北京分公司招商部',
        },
      ],
      empNameOptions: [
        {
          value: '0',
          label: '张三丰',
        },
        {
          value: '1',
          label: '张四丰',
        },
        {
          value: '2',
          label: '张五丰',
        },
      ],
      //搜索数据
      searchData: {
        pageNum: 1,
        pageSize: 5,
        authorizationPut: '',
      },
      //渲染数据
      tableData: [],
      //新增弹窗
      addDialog: false,
      newPlanDialog: false,
      editDialog: false,
      editPlanDialog: false,
      delDialog: false,
      //新增数据
      addData: {
        authorizationGet: '',
        authorizationPut: '',
        empName: '',
      },
      //编辑数据
      editData: {
        authorizationGet: '',
        authorizationPut: '',
        empName: '',
        id: 0,
      },
      //删除id
      deleteId: 0,
    };
  },
  methods: {
    //渲染
    getData: function () {
      this.$axios({
        url: '/smart/plan/authorization/query',
        params: this.searchData,
      }).then(({ data }) => {
        if (data.code == '00000') {
          if (data.data.pageInfo.list.length > 0) {
            this.tableData = data.data.pageInfo.list;
            this.total = data.data.pageInfo.total;
          } else {
            this.tableData = data.data.pageInfo.list;
            this.$message.error('暂无数据！');
          }
        } else {
          this.$message.error(data.msg);
        }
      });
    },
    //搜索
    onSearch: function () {
      this.getData();
    },
    //重置
    onReset: function () {
      this.searchData = {
        pageNum: 1,
        pageSize: 10,
        authorizationPut: '',
      };
      this.getData();
    },
    //添加
    addOpen: function () {
      this.addData = {
        authorizationGet: '',
        authorizationPut: '',
        empName: '',
      };
      this.addDialog = true;
    },
    //添加保存
    addSave: function () {
      this.$axios({
        url: '/smart/plan/authorization/add',
        method: 'post',
        data: this.addData,
      }).then(({ data }) => {
        if (data.code == '00000') {
          this.addDialog = false;
          this.$message.success(data.msg);
          this.getData();
        } else {
          this.$message.error(data.msg);
        }
      });
    },
    //编辑
    editOpen: function (row) {
      this.editDialog = true;
      this.editData = row;
    },
    //编辑保存
    editSave: function () {
      this.$axios({
        url: '/smart/plan/authorization/update',
        method: 'put',
        data: this.editData,
      }).then(({ data }) => {
        if (data.code == '00000') {
          this.editDialog = false;
          this.$message.success(data.msg);
          this.getData();
        } else {
          this.$message.error(data.msg);
        }
      });
    },
    //删除弹窗
    delOpen: function (row) {
      this.deleteId = row.id;
      this.delDialog = true;
    },
    delSave: function () {
      this.$axios({
        url: '/smart/plan/authorization/del/' + this.deleteId,
        method: 'delete',
      }).then(({ data }) => {
        if (data.code == '00000') {
          this.delDialog = false;
          this.$message.success(data.msg);
          this.getData();
        } else {
          this.$message.error(data.msg);
        }
      });
    },
    //分页
    handleSizeChange(val) {
      this.searchData.pageSize = val;
      this.getData();
    },
    handleCurrentChange(val) {
      this.searchData.pageNum = val;
      this.getData();
    },
  },
  mounted: function () {
    this.getData();
  },
};
</script>

<style lang="less" scoped>
.container {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  .container-title {
    width: 98%;
    height: 80px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    p {
      color: #333333;
      font-size: 16px;
      font-weight: 400;
      font-family: 'Arial Normal', 'Arial';
    }
    .el-breadcrumb-item {
      color: rgba(148, 148, 148, 0.898039215686275);
      font-size: 12px;
      font-weight: 400;
      font-family: 'Arial Normal', 'Arial';
    }
  }
  .search-box {
    width: 98%;
    height: 80px;
    background: #ffffff;
    display: flex;
    justify-content: flex-start;
    align-items: center;
    border: 1px solid #ffffff;
    border-radius: 5px;
    .el-form {
      width: 98%;
      background: #ffffff;
      display: flex;
      justify-content: flex-start;
      align-items: center;
      .el-form-item {
        margin-left: 10px;
        margin-bottom: 0;
        .el-select {
          width: 155px;
        }
        .el-button {
          margin: 10px;
        }
        .reset {
          background-color: white;
          color: #c0c4cc;
          border: 1px solid #c0c4cc;
        }
        .reset:hover {
          color: #0bb197;
        }
      }
      .block {
        margin-left: 10px;
      }
      .el-date-editor--datetimerange.el-input__inner {
        width: 310px;
      }
    }
  }
  .data-list {
    width: 98%;
    height: 100%;
    background: #ffffff;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: space-around;
    margin-top: 20px;
    .data-list-title {
      width: 98%;
      height: 60px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      span {
        height: 16px;
        font-family: 'Arial Normal', 'Arial';
        font-weight: 700;
        font-style: normal;
        font-size: 13px;
        color: #3c4145;
      }
    }
    .el-table {
      flex: 1;
      .el-button {
        color: #0bb197;
      }
    }
    .el-pagination {
      width: 98%;
      height: 60px;
      display: flex;
      justify-content: flex-end;
      align-items: center;
    }
  }
}
</style>
<style lang="less">
.el-pagination.is-background .el-pager li:not(.disabled):hover {
  color: #0bb197;
}
.el-pagination.is-background .el-pager li:not(.disabled).active {
  background-color: #0bb197;
  color: #fff;
}
.edit .el-dialog,
.add .el-dialog {
  display: flex;
  flex-direction: column;
  align-items: center;
  .el-dialog__header {
    width: 95%;
    height: 60px;
    padding: 0;
    .el-dialog__title {
      line-height: 60px;
      font-size: 15px;
      font-weight: bold;
    }
  }
  .el-dialog__body {
    width: 95%;
    padding: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    .el-form {
      width: 95%;
      display: flex;
      flex-direction: column;
      align-items: center;
      .authorized-department {
        width: 95%;
        display: flex;
        justify-content: space-between;
        align-items: center;
        .el-form-item {
          width: 48%;
          .el-select {
            width: 100%;
          }
        }
      }
      .authorized-personnel {
        width: 95%;
        display: flex;
        justify-content: space-between;
        align-items: center;
        .el-form-item {
          width: 100%;
          .el-select {
            width: 100%;
          }
        }
      }
    }
  }
  .el-dialog__footer {
    width: 95%;
    height: 80px;
    padding: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    .dialog-footer {
      width: 50%;
      display: flex;
      justify-content: space-around;
      align-items: center;
      .save {
        width: 66px;
        height: 30px;
        background: #0bb197;
        display: flex;
        justify-content: center;
        align-items: center;
      }
      .cancel {
        width: 66px;
        height: 30px;
        background: #dfe1e8;
        display: flex;
        justify-content: center;
        align-items: center;
      }
      .save:focus,
      .save:hover {
        color: #ffffff;
        border-color: #c6e2ff;
      }
      .cancel:focus,
      .cancel:hover {
        color: #333333;
        border-color: #dfe1e8;
      }
    }
  }
}
.del .el-dialog {
  .el-dialog__body {
    display: flex;
    justify-content: center;
  }
  .el-dialog__footer {
    width: 95%;
    height: 80px;
    padding: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    .dialog-footer {
      width: 50%;
      display: flex;
      justify-content: space-around;
      align-items: center;
      .save {
        width: 66px;
        height: 30px;
        background: #0bb197;
        display: flex;
        justify-content: center;
        align-items: center;
      }
      .cancel {
        width: 66px;
        height: 30px;
        background: #dfe1e8;
        display: flex;
        justify-content: center;
        align-items: center;
      }
      .save:focus,
      .save:hover {
        color: #ffffff;
        border-color: #c6e2ff;
      }
      .cancel:focus,
      .cancel:hover {
        color: #333333;
        border-color: #dfe1e8;
      }
    }
  }
}
</style>
